/**
 * 
 */
package com.gragra.sampling.nonproj;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import com.gragra.sampling.PopulationProbabilityAssigner;
import com.gragra.sampling.PopulationSamplingStructure;
import com.gragra.sampling.binary.ConvertSample;
import com.gragra.sampling.binary.HeadAssignmentStructure;
import com.gragra.sampling.binary.ThreadedToString;
/**
 * @author Christoph Teichmann
 * 22.09.2013
 */
public class ConvertHeadedStructure implements ConvertSample
{
	/**
	 * 
	 */
	private String best = null;
	/* (non-Javadoc)
	 * @see com.gragra.sampling.binary.ConvertSample#getBest(com.gragra.sampling.PopulationSamplingStructure, com.gragra.sampling.PopulationProbabilityAssigner)
	 */
	@Override
	public String getBest(PopulationSamplingStructure str,
			PopulationProbabilityAssigner pa)
	{return best;}
	/**
	 * 
	 */
	private Object2IntOpenHashMap<String> counts = null;
	/* (non-Javadoc)
	 * @see com.gragra.sampling.binary.ConvertSample#minRisk(com.gragra.sampling.PopulationSamplingStructure, com.gragra.sampling.PopulationProbabilityAssigner)
	 */
	@Override
	public String minRisk(PopulationSamplingStructure str,
			PopulationProbabilityAssigner pa)
	{return selectMostFrequent(counts);}
	/**
	 * 
	 * @param counts2
	 * @return
	 */
	private String selectMostFrequent(Object2IntOpenHashMap<String> c)
	{
		int max = 0;
		String best = null;
		for(String s : c.keySet())
		{
			int val = c.getInt(s);
			if(val > max)
			{
				max = val;
				best = s;
			}
		}
		return best;
	}
	/* (non-Javadoc)
	 * @see com.gragra.sampling.binary.ConvertSample#addSample(com.gragra.sampling.PopulationSamplingStructure)
	 */
	@Override
	public void addSample(PopulationSamplingStructure str)
	{
		if(this.counts == null)
		{this.counts = new Object2IntOpenHashMap<String>();}
		ThreadedToString tts = (ThreadedToString) str;
		for(int thread=0;thread<tts.getNumberOfThreads();++thread)
		{counts.addTo(convert(str,thread), 1);}
	}
	/**
	 * 
	 * @param str
	 * @return
	 */
	private String convert(PopulationSamplingStructure str, int thread)
	{
		HeadAssignmentStructure has = (HeadAssignmentStructure) str;
		StringBuilder sb = new StringBuilder();
		for(int pos=0;pos<has.getLength();++pos)
		{
			if(pos != 0)
			{sb.append(" ");}
			sb.append(has.getHeadPosition(pos, thread));
		}
		return sb.toString();
	}
	/* (non-Javadoc)
	 * @see com.gragra.sampling.binary.ConvertSample#setBest(com.gragra.sampling.PopulationSamplingStructure, int)
	 */
	@Override
	public void setBest(PopulationSamplingStructure str, int thread)
	{this.best = this.convert(str, thread);}
	/* (non-Javadoc)
	 * @see com.gragra.sampling.binary.ConvertSample#getNumberSeen(java.lang.String)
	 */
	@Override
	public double getNumberSeen(String gold)
	{return this.counts.getInt(gold);}
	/**
	 * 
	 * @return
	 */
	public Object2IntOpenHashMap<String> getCounts()
	{return this.counts;}
}